﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using zichan.BLL;
using zichan.Entity;
using zichan.Common;
using zichan.Models;
using AutoMapper;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;

namespace zichan.Controllers
{
    public class CabinetController : BaseController
    {
        // GET: Cabinet
        public ActionResult Index(string name, string Ip, int? machineRoomId, int? cabinetClassId,int? serverPurposeSubId, string keyWord, string code, string uNO, int? vacancy, int? companyId, int? statu, int? cabinetId, int? page, int? pageSize, int? pageCount, int? selectedPage, int? isNull)
        {
            page = page ?? 1;
            pageSize = pageSize ?? 10;
            if (selectedPage.HasValue)
                page = selectedPage;
            if (pageCount.HasValue)
                page = page <= pageCount ? page : pageCount;
            if (page < 1) page = 1;
            ViewBag.page = page;
            ViewBag.pageSize = pageSize;
            ViewBag.selectedPage = selectedPage;
            List<SelectListItem> pitems = new List<SelectListItem> { new SelectListItem { Value = "10", Text = "10" }, new SelectListItem { Value = "20", Text = "20" }, new SelectListItem { Value = "50", Text = "50" }, new SelectListItem { Value = "100", Text = "100" } };
            ViewBag.pageSizes = new SelectList(pitems, "Value", "Text", ViewBag.pageIndex);

            ViewBag.isNull = isNull;
            ViewBag.name = name;
            ViewBag.Ip = Ip;
            ViewBag.cabinetId = cabinetId;
            ViewBag.machineRoomId = machineRoomId;
            ViewBag.machineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name", ViewBag.machineRoomId);
            ViewBag.cabinetClassId = cabinetClassId;
            ViewBag.cabinetClassIds = new SelectList(ServerPurposeService.GetList(), "serverPurposeId", "serverPurposeName", cabinetClassId);
            ViewBag.serverPurposeSubId = serverPurposeSubId;
            ViewBag.serverPurposeSubIds = new SelectList(ServerPurposeSubClassService.GetList(), "serverPurposeSubId", "serverPurposeSubName", serverPurposeSubId);
            ViewBag.keyWord = keyWord;
            ViewBag.code = code;
            ViewBag.uNO = uNO;
            ViewBag.vacancy = vacancy;
            List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Value = "0", Text = "无空位" }, new SelectListItem { Value = "1", Text = "有空位" } };
            ViewBag.vacancys = new SelectList(items, "Value", "Text", vacancy);
            ViewBag.companyId = companyId;
            ViewBag.companyIds = new SelectList(CompanyClassService.GetCompanyList(), "Id", "Name", companyId);
            List<SelectListItem> statuItems = new List<SelectListItem> { new SelectListItem { Value = "0", Text = "空闲" }, new SelectListItem { Value = "1", Text = "上架" }, new SelectListItem { Value = "2", Text = "下架" } };
            ViewBag.statu = statu;
            ViewBag.status = new SelectList(statuItems, "Value", "Text");
            return View(CabinetManageService.List(name, Ip, machineRoomId, cabinetClassId, keyWord, code, uNO, vacancy, companyId, statu, cabinetId, isNull,serverPurposeSubId, page.Value, pageSize.Value));
        }
        public ActionResult Create()
        {
            ViewBag.MachineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name");
            return View();
        }
        [HttpPost]
        public ActionResult Create(Cabinet model)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.MachineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name", model.MachineRoomId);
                return View(model);
            }
            model.uploadTime = DateTime.Now;
            string err = CabinetManageService.AddCabinet(model);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#添加成功";
                CabinetUseLog log = new CabinetUseLog();
                log.Time = DateTime.Now;
                log.cabinetId = model.Id;
                log.Operation = "添加机柜：" + model.Name;
                CabinetUseLogService.Add(log);
                MachineRoomLogService.Add(3, log.Operation);
            }
            else
            {
                TempData["alert"] = "error#" + err;
            }
            return RedirectToAction("Index");
        }
        /// <summary>
        /// 上架服务器
        /// </summary>
        /// <returns></returns>
        public ActionResult PutAway(int? machineRoomId, int? cabinetId)
        {
           
            ViewBag.machineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name",machineRoomId);
            if (machineRoomId.HasValue && cabinetId.HasValue)
                ViewBag.cabinetIds = new SelectList(CabinetManageService.CabinetList(machineRoomId.Value), "Id", "Name", cabinetId);
            else
            {
                List<SelectListItem> cItems = new List<SelectListItem>();
                ViewBag.cabinetIds = new SelectList(cItems);
            }
            ViewBag.companyIds = new SelectList(CompanyClassService.GetCompanyList(), "Id", "Name");
            //ViewBag.cabinetClassIds = new SelectList(CabinetClassService.List(), "Id", "Purpose");
            ViewBag.serverPurposeIds = new SelectList(ServerPurposeService.GetList(), "serverPurposeId", "serverPurposeName");
            ViewBag.ipIds = new SelectList(IPService.NoUseList(), "Id", "Ip");
            ViewBag.subIpIds = new SelectList(IPService.NoUseList(), "Id", "Ip");
            List<SelectListItem> uNO = new List<SelectListItem>();
            ViewBag.uNOs = new SelectList(uNO);
            List<SelectListItem> code = new List<SelectListItem>();
            ViewBag.Codes = new SelectList(code);
            //CabinetClass c = CabinetClassService.GetCabinetClass(1);
            ServerPurposeSubClass c = ServerPurposeSubClassService.Get(1);
            List<SelectListItem> citems = new List<SelectListItem> { new SelectListItem { Value = c.IsSendProperty, Text = "送产权" }, new SelectListItem { Value = c.IsNoProperty, Text = "无产权" } };
            ViewBag.Propertys = new SelectList(citems, "Value", "Text");
            //CabinetClass Tc = CabinetClassService.GetCabinetClass(2);
            ServerPurposeSubClass Tc = ServerPurposeSubClassService.Get(2);
            List<SelectListItem> Tcitems = new List<SelectListItem> { new SelectListItem { Value = Tc.IsSendProperty, Text = "送产权" }, new SelectListItem { Value = Tc.IsNoProperty, Text = "无产权" } };
            ViewBag.TPropertys = new SelectList(Tcitems, "Value", "Text");
            //List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Value = "0", Text = "无产权" }, new SelectListItem { Value = "1", Text = "送产权" } };
            //ViewBag.equitys = new SelectList(items, "Value", "Text");
            ModelState.Clear();
            return View();
        }
        [HttpPost]
        public ActionResult PutAway(PutAwayServerModel model)
        {
            int u_Number = ServerInfoService.Get(model.Code);
            if (model.Vacancy < u_Number)
            {
                TempData["alert"] = "error#服务器U数大于机柜剩余U数，无法上架";
                return View(model);
            }
            if (!ModelState.IsValid)
            {
                ViewBag.machineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name", model.MachineRoomId);
                if (model.MachineRoomId != 0)
                    ViewBag.cabinetIds = new SelectList(CabinetManageService.CabinetList(model.MachineRoomId), "Id", "Name", model.CabinetId);
                else
                {
                    List<SelectListItem> cItems = new List<SelectListItem>();
                    ViewBag.cabinetIds = new SelectList(cItems);
                }
                ViewBag.Codes = new SelectList(AssetService.codeList(model.CompanyId));
                ViewBag.companyIds = new SelectList(CompanyClassService.GetCompanyList(), "Id", "Name", model.CompanyId);
                //ViewBag.cabinetClassIds = new SelectList(CabinetClassService.List(), "Id", "Purpose", model.PurposeId);
                ViewBag.serverPurposeIds = new SelectList(ServerPurposeService.GetList(), "serverPurposeId", "serverPurposeName");
                ViewBag.ipIds = new SelectList(IPService.NoUseList(), "Id", "Ip", model.IPId);
                ViewBag.subIpIds = new SelectList(IPService.NoUseList(), "Id", "Ip", model.SubIPId);
                //List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Value = "0", Text = "无产权" }, new SelectListItem { Value = "1", Text = "送产权" } };
                //ViewBag.equitys = new SelectList(items, "Value", "Text", model.Equity);
                if (model.UseNumber <= 0)
                {
                    List<SelectListItem> uNO = new List<SelectListItem>();
                    ViewBag.uNOs = new SelectList(uNO);
                }
                else
                {
                    ViewBag.uNOs = new SelectList(CabinetManageService.useNO(model.CabinetId), model.UseNumber);
                }
                if (string.IsNullOrEmpty(model.Code))
                {
                    List<SelectListItem> code = new List<SelectListItem>();
                    ViewBag.Codes = new SelectList(code);
                }
                else
                {
                    ViewBag.Codes = new SelectList(AssetService.codeList(model.CompanyId), model.Code);
                }
                // CabinetClass c = CabinetClassService.GetCabinetClass(1);
                ServerPurposeSubClass c = ServerPurposeSubClassService.Get(1);
                List<SelectListItem> citems = new List<SelectListItem> { new SelectListItem { Value = c.IsSendProperty, Text = "送产权" }, new SelectListItem { Value = c.IsNoProperty, Text = "无产权" } };
                ViewBag.Propertys = new SelectList(citems, "Value", "Text", model.Property);
                //CabinetClass Tc = CabinetClassService.GetCabinetClass(2);
                ServerPurposeSubClass Tc = ServerPurposeSubClassService.Get(2);
                List<SelectListItem> Tcitems = new List<SelectListItem> { new SelectListItem { Value = Tc.IsSendProperty, Text = "送产权" }, new SelectListItem { Value = Tc.IsNoProperty, Text = "无产权" } };
                ViewBag.TPropertys = new SelectList(Tcitems, "Value", "Text");
                TempData["alert"] = "error#带有红色星号的字段为必填字段，请确认数据的完整性";
                return View(model);
            }

            if (model.serverPurposeSubId != 1)
            {
                model.Property = null;
            }
            if (model.serverPurposeSubId != 2)
            {
                model.TProperty = null;
            }
            var mapperconfig = new MapperConfiguration(cfg => { cfg.CreateMap<PutAwayServerModel, PutawayServer>(); });
            IMapper mapper = mapperconfig.CreateMapper();
            PutawayServer server = mapper.Map<PutAwayServerModel, PutawayServer>(model);
            string err = CabinetManageService.AddPutAway(server);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#上架成功";
                CabinetUseLog log = new CabinetUseLog();
                log.Time = DateTime.Now;
                log.cabinetId = model.CabinetId;
                log.Operation = "服务器上架：" + model.Code + "-" + model.UseNumber + "U-" + CompanyClassService.GetCompany(model.CompanyId).Name + "-" + ServerPurposeService.Get(model.serverPurposeId).serverPurposeName + "-" + IPService.Get(model.IPId).Ip + "-" + IPService.GetSubIp(model.SubIPIds);
                CabinetUseLogService.Add(log);
                MachineRoomLogService.Add(3, log.Operation);
            }
            else
            {
                TempData["alert"] = "error#" + err;
            }
            return RedirectToAction("index");
        }

        public ActionResult PutAwayEdit(int id)
        {
            PutawayServer model = CabinetManageService.Get(id);
            List<string> codes = AssetService.codeList(model.CompanyId.Value);
            codes.Add(model.Code);
            ViewBag.Codes = new SelectList(codes, model.Code);
            ViewBag.machineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name", model.MachineRoomId);

            ViewBag.cabinetIds = new SelectList(CabinetManageService.CabinetList(model.MachineRoomId), "Id", "Name", model.CabinetId);

            ViewBag.uNOs = new SelectList(CabinetManageService.useNO(model.CabinetId), model.UseNumber);
            //CabinetClass c = CabinetClassService.GetCabinetClass(1);
            ServerPurposeSubClass c = ServerPurposeSubClassService.Get(1);
            List<SelectListItem> citems = new List<SelectListItem> { new SelectListItem { Value = c.IsSendProperty, Text = "送产权" }, new SelectListItem { Value = c.IsNoProperty, Text = "无产权" } };
            ViewBag.Propertys = new SelectList(citems, "Value", "Text", model.Property);
            //CabinetClass Tc = CabinetClassService.GetCabinetClass(2);
            ServerPurposeSubClass Tc = ServerPurposeSubClassService.Get(2);
            List<SelectListItem> Tcitems = new List<SelectListItem> { new SelectListItem { Value = Tc.IsSendProperty, Text = "送产权" }, new SelectListItem { Value = Tc.IsNoProperty, Text = "无产权" } };
            ViewBag.TPropertys = new SelectList(Tcitems, "Value", "Text", model.TProperty);
            ViewBag.companyIds = new SelectList(CompanyClassService.GetCompanyList(), "Id", "Name", model.CompanyId);
            //ViewBag.cabinetClassIds = new SelectList(CabinetClassService.List(), "Id", "Purpose", model.PurposeId);
            ViewBag.serverPurposeIds = new SelectList(ServerPurposeService.GetList(), "serverPurposeId", "serverPurposeName");
            ViewBag.ipIds = new SelectList(IPService.IpForEditList(model.MachineRoomId,id), "Id", "Ip", model.IPId);
            ViewBag.subIpIds = new SelectList(IPService.SubIpForEditList(model.MachineRoomId, id), "Id", "Ip", model.SubIPId);
            //List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Value = "0", Text = "无产权" }, new SelectListItem { Value = "1", Text = "送产权" } };
            //ViewBag.equitys = new SelectList(items, "Value", "Text", model.Equity);
            var mapperconfig = new MapperConfiguration(cfg => { cfg.CreateMap<PutawayServer, PutAwayServerModel>(); });
            IMapper mapper = mapperconfig.CreateMapper();
            PutAwayServerModel serverModel = mapper.Map<PutawayServer, PutAwayServerModel>(model);
            return View(serverModel);
        }
        [HttpPost]
        public ActionResult PutAwayEdit(PutAwayServerModel model)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.machineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name", model.MachineRoomId);
                if (model.MachineRoomId != 0)
                    ViewBag.cabinetIds = new SelectList(CabinetManageService.CabinetList(model.MachineRoomId), "Id", "Name", model.CabinetId);
                else
                {
                    List<SelectListItem> cItems = new List<SelectListItem>();
                    ViewBag.cabinetIds = new SelectList(cItems);
                }

                ViewBag.companyIds = new SelectList(CompanyClassService.GetCompanyList(), "Id", "Name", model.CompanyId);
                //ViewBag.cabinetClassIds = new SelectList(CabinetClassService.List(), "Id", "Purpose", model.PurposeId);
                ViewBag.serverPurposeIds = new SelectList(ServerPurposeService.GetList(), "serverPurposeId", "serverPurposeName");
                ViewBag.ipIds = new SelectList(IPService.IpForEditList(model.MachineRoomId,model.Id), "Id", "Ip", model.IPId);
                ViewBag.subIpIds = new SelectList(IPService.SubIpForEditList(model.MachineRoomId,model.Id), "Id", "Ip", model.SubIPId);
                //List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Value = "0", Text = "无产权" }, new SelectListItem { Value = "1", Text = "送产权" } };
                //ViewBag.equitys = new SelectList(items, "Value", "Text", model.Equity);
                if (model.UseNumber <= 0)
                {
                    List<SelectListItem> uNO = new List<SelectListItem>();
                    ViewBag.uNOs = new SelectList(uNO);
                }
                else
                {
                    ViewBag.uNOs = new SelectList(CabinetManageService.useNO(model.CabinetId), model.UseNumber);
                }
                List<string> codes = AssetService.codeList(model.CompanyId);
                codes.Add(model.Code);
                ViewBag.Codes = new SelectList(codes, model.Code);
                ServerPurposeSubClass c = ServerPurposeSubClassService.Get(1);
                List<SelectListItem> citems = new List<SelectListItem> { new SelectListItem { Value = c.IsSendProperty, Text = "送产权" }, new SelectListItem { Value = c.IsNoProperty, Text = "无产权" } };
                ViewBag.Propertys = new SelectList(citems, "Value", "Text", model.Property);
                ServerPurposeSubClass Tc = ServerPurposeSubClassService.Get(2);
                List<SelectListItem> Tcitems = new List<SelectListItem> { new SelectListItem { Value = Tc.IsSendProperty, Text = "送产权" }, new SelectListItem { Value = Tc.IsNoProperty, Text = "无产权" } };
                ViewBag.TPropertys = new SelectList(Tcitems, "Value", "Text", model.TProperty);
                TempData["alert"] = "error#带有红色星号的字段为必填字段，请确认数据的完整性";
                return View(model);
            }
            if (model.serverPurposeSubId != 1)
            {
                model.Property = null;
            }
            if (model.serverPurposeSubId != 2)
            {
                model.TProperty = null;
            }
            if (model.SubIPId != null)
                model.SubIPIds = string.Join(",", model.SubIPId);
            PutawayServer server = CabinetManageService.Get(model.Id);
            string mName = server.MachineRoomName;
            string oldcName = server.CabinetName;
            string oldcompanyName = server.CompanyName;
            string purpose = server.serverPurposeName;
            int oldmId = server.MachineRoomId;
            int oldcId = server.CabinetId;
            int oldcompanyId = server.CompanyId.Value;
            int purposeId = model.serverPurposeId;
            string oldcode = server.Code;
            int oldzw = server.UseNumber.Value;
            string oldIP = server.IP;
            string oldsubid = server.SubIPIds;
            string oldinIp = server.IntranetIP;
            bool oldbool = server.IsInverse;
            string oldRemark = server.Remark;
            var mapperconfig = new MapperConfiguration(cfg => { cfg.CreateMap<PutAwayServerModel, PutawayServer>(); });
            IMapper mapper = mapperconfig.CreateMapper();
            PutawayServer putawayServer = mapper.Map<PutAwayServerModel, PutawayServer>(model);
            string err = CabinetManageService.EditPutAway(putawayServer);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#修改成功";
                CabinetUseLog log = new CabinetUseLog();
                log.cabinetId = model.CabinetId;
                log.Time = DateTime.Now;
                if (oldmId != model.MachineRoomId)
                {
                    log.Operation = "编辑>机房>原：" + mName + "修改为：" + MachineRoomService.Get(model.MachineRoomId).Name;
                    CabinetUseLogService.Add(log);
                    MachineRoomLogService.Add(3, log.Operation);
                }
                if (oldcId != model.CabinetId)
                {
                    log.Operation = "编辑>机柜>原：" + oldcName + "修改为：" + CabinetManageService.GetCabinet(model.CabinetId).Name;
                    CabinetUseLogService.Add(log);
                    MachineRoomLogService.Add(3, log.Operation);
                }
                if (oldcompanyId != model.CompanyId)
                {
                    log.Operation = "编辑>公司>原：" + oldcompanyName + "修改为：" + CompanyClassService.GetCompany(model.CompanyId).Name;
                    CabinetUseLogService.Add(log);
                    MachineRoomLogService.Add(3, log.Operation);
                }
                if (purposeId != model.serverPurposeId)
                {
                    log.Operation = "编辑>用途>原：" + purpose + "修改为：" + ServerPurposeService.Get(model.serverPurposeId).serverPurposeName;
                    CabinetUseLogService.Add(log);
                    MachineRoomLogService.Add(3, log.Operation);
                }
                if (oldcode != model.Code)
                {
                    log.Operation = "编辑>编码>原：" + oldcode + "修改为：" + model.Code;
                    CabinetUseLogService.Add(log);
                    MachineRoomLogService.Add(3, log.Operation);
                }
                if (oldzw != model.UseNumber)
                {
                    log.Operation = "编辑>占位>原：" + oldzw + "U修改为：" + model.UseNumber + "U";
                    CabinetUseLogService.Add(log);
                    MachineRoomLogService.Add(3, log.Operation);
                }
                if (oldIP != model.IP)
                {
                    log.Operation = "编辑>IP>原：" + oldIP + "修改为：" + model.IP;
                    CabinetUseLogService.Add(log);
                    MachineRoomLogService.Add(3, log.Operation);
                }
                if (oldsubid != model.SubIPIds)
                {
                    string oldIps = string.Join(",", CabinetManageService.IPs(oldsubid));
                    string Ips = string.Join(",", CabinetManageService.IPs(model.SubIPIds));
                    //var subIp = oldIps.Intersect(Ips);
                    //var oldIps1 = oldIps.Except(subIp);
                    //var Ips1 = Ips.Except(subIp);

                    log.Operation = "编辑>机柜>原：" + oldIps + "修改为：" + Ips;
                    CabinetUseLogService.Add(log);
                    MachineRoomLogService.Add(3, log.Operation);
                }
                if (oldinIp != model.IntranetIP)
                {
                    log.Operation = "编辑>内网IP>原：" + oldinIp + "修改为：" + model.IntranetIP;
                    CabinetUseLogService.Add(log);
                    MachineRoomLogService.Add(3, log.Operation);
                }
                if (oldbool != model.IsInverse)
                {
                    log.Operation = "编辑>IP反解>原：" + oldbool + "修改为：" + model.IsInverse;
                    CabinetUseLogService.Add(log);
                    MachineRoomLogService.Add(3, log.Operation);
                }
                if (oldmId != model.MachineRoomId)
                {
                    log.Operation = "编辑>备注>原：" + oldRemark + "修改为：" + model.Remark;
                    CabinetUseLogService.Add(log);
                    MachineRoomLogService.Add(3, log.Operation);
                }

            }
            else
            {
                TempData["alert"] = "error#" + err;
            }
            return RedirectToAction("Index");
        }
        public ActionResult CabinetEdit(int id)
        {
            Cabinet model = CabinetManageService.GetCabinet(id);
            ViewBag.MachineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name", model.MachineRoomId);
            return View(model);
        }
        [HttpPost]
        public ActionResult CabinetEdit(Cabinet model)
        {
            if (!ModelState.IsValid)
            {
                ViewBag.MachineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name", model.MachineRoomId);
                return View(model);
            }
            string err = CabinetManageService.EditCabinet(model);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#修改成功";

            }
            else
            {
                TempData["alert"] = "error#" + err;
            }
            return RedirectToAction("Index");
        }
        public ActionResult Delete(int id)
        {
            string err = CabinetManageService.RemoveCabinet(id);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#删除成功";
            }
            else
            {
                TempData["alert"] = "error#" + err;
            }
            return RedirectToAction("Index");
        }
        public ActionResult Cancellation(int id)
        {
            int cabinetId = CabinetManageService.Get(id).CabinetId;
            Cabinet model = CabinetManageService.GetCabinet(cabinetId);
            model.IsDelete = 1;
            string err = CabinetManageService.CancellationCabinet(model);
            if (string.IsNullOrEmpty(err))
            {

                string err2 = CabinetManageService.RemovePutAwayServer(id);
                if (string.IsNullOrEmpty(err2))
                {
                    TempData["alert"] = "success#已报废";
                }
                else
                    TempData["alert"] = "error#无法报废";
            }
            else
                TempData["alert"] = "error#" + err;
            return RedirectToAction("Index");
        }
        public ActionResult Detail(int id)
        {
            PutawayServer server = CabinetManageService.Get(id);
            Models.DetailModel model = new Models.DetailModel();
            model.cabinet = CabinetManageService.GetCabinet(server.CabinetId);
            model.cabinet.MachineRoomName = server.MachineRoomName;
            model.statu = server.Statu;
            model.noUse = server.Vacancy.Value;
            model.Id = server.Id;
            model.cabinetId = server.CabinetId;

            return View(model);
        }

        public ActionResult OutOfRock(int id, int cabinetId)
        {
            zichan.Models.OutOfRockModel model = new Models.OutOfRockModel();
            model.Id = id;
            model.cabinetId = cabinetId;
            return View(model);
        }

        [HttpPost]
        public ActionResult OutOfRock(Models.OutOfRockModel model)
        {
            PutawayServer server = CabinetManageService.Get(model.Id);
            server.RemoveRemark = model.Remark;
            server.Statu = (int)ServerStatu.Remove;
            string err = CabinetManageService.OutOfRockEdit(server);
            if (string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#成功下架";
                CabinetUseLog log = new CabinetUseLog();
                log.cabinetId = server.CabinetId;
                log.Time = DateTime.Now;
                log.Operation = "服务器下架：" + server.Code + "-" + server.UseNumber + "U-" + CompanyClassService.GetCompany(server.CompanyId.Value).Name + "-" + (ServerPurposeService.Get(server.serverPurposeId)==null? " ": ServerPurposeService.Get(server.serverPurposeId).serverPurposeName) + "-" + IPService.Get(server.IPId.Value).Ip + "-" + IPService.GetSubIp(server.SubIPIds);
                CabinetUseLogService.Add(log);
                MachineRoomLogService.Add(3, log.Operation);
            }
            else
            {
                TempData["alert"] = "error#" + err;
            }
            return RedirectToAction("Index");
        }
        #region 可删
        public ActionResult OldPutServer(int? machineRoomId, int? cabinetId)
        {
            ViewBag.machineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name", machineRoomId);
            if (machineRoomId.HasValue && cabinetId.HasValue)
                ViewBag.cabinetIds = new SelectList(CabinetManageService.CabinetList(machineRoomId.Value), "Id", "Name", cabinetId);
            else
            {
                List<SelectListItem> cItems = new List<SelectListItem>();
                ViewBag.cabinetIds = new SelectList(cItems);
            }
            ViewBag.userIds = new SelectList(StaffService.StaffAll(1), "Id", "Name");
            ViewBag.companyIds = new SelectList(CompanyClassService.GetCompanyList(), "Id", "Name");
            ViewBag.cabinetClassIds = new SelectList(CabinetClassService.List(), "Id", "Purpose");
            ViewBag.ipIds = new SelectList(IPService.NoUseList(), "Id", "Ip");
            ViewBag.subIpIds = new SelectList(IPService.NoUseList(), "Id", "Ip");
            ViewBag.serverPurposeIds = new SelectList(ServerPurposeService.GetList(), "serverPurposeId", "serverPurposeName");
            //ViewBag.serverPurposeSubIds = new SelectList(ServerPurposeSubClassService.GetList(), "serverPurposeSubId", "serverPurposeSubName");
            //List<SelectListItem> uNO = new List<SelectListItem>();
            //ViewBag.uNOs = new SelectList(uNO);
            //List<SelectListItem> code = new List<SelectListItem>();
            //ViewBag.Codes = new SelectList(code);
            CabinetClass c = CabinetClassService.GetCabinetClass(1);
            List<SelectListItem> citems = new List<SelectListItem> { new SelectListItem { Value = c.IsSendProperty, Text = "送产权" }, new SelectListItem { Value = c.IsNoProperty, Text = "无产权" } };
            ViewBag.Propertys = new SelectList(citems, "Value", "Text");
            CabinetClass Tc = CabinetClassService.GetCabinetClass(2);
            List<SelectListItem> Tcitems = new List<SelectListItem> { new SelectListItem { Value = Tc.IsSendProperty, Text = "送产权" }, new SelectListItem { Value = Tc.IsNoProperty, Text = "无产权" } };
            ViewBag.TPropertys = new SelectList(Tcitems, "Value", "Text");
            //List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Value = "0", Text = "无产权" }, new SelectListItem { Value = "1", Text = "送产权" } };
            //ViewBag.equitys = new SelectList(items, "Value", "Text");
            List<string> ss = new List<string> { AsseteClassService.GetAsseteClass(1).Identity, AsseteClassService.GetAsseteClass(1).SubIdentity };
            ViewBag.Identitys = ss;
            ModelState.Clear();
            return View();
        }
        [HttpPost]
        public ActionResult OldPutServer(OldPutawayModel model)
        {
            if(!ModelState.IsValid||model.UseNumber>AssetService.oldCabinetNum(model.CabinetId))
            {
                if (model.UseNumber > AssetService.oldCabinetNum(model.CabinetId))
                    TempData["alert"] = "error#服务器U数大于机柜架可使用U数";
                ViewBag.machineRoomIds = new SelectList(MachineRoomService.List(), "Id", "Name", model.MachineRoomId);
                if (model.MachineRoomId != 0)
                    ViewBag.cabinetIds = new SelectList(CabinetManageService.CabinetList(model.MachineRoomId), "Id", "Name", model.CabinetId);
                else
                {
                    List<SelectListItem> cItems = new List<SelectListItem>();
                    ViewBag.cabinetIds = new SelectList(cItems);
                }
                ViewBag.userIds = new SelectList(StaffService.StaffAll(1), "Id", "Name",model.UserId);
                ViewBag.Codes = new SelectList(AssetService.codeList(model.CompanyId));
                ViewBag.companyIds = new SelectList(CompanyClassService.GetCompanyList(), "Id", "Name", model.CompanyId);
                ViewBag.cabinetClassIds = new SelectList(CabinetClassService.List(), "Id", "Purpose", model.PurposeId);
                ViewBag.ipIds = new SelectList(IPService.NoUseList(), "Id", "Ip", model.IPId);
                ViewBag.subIpIds = new SelectList(IPService.NoUseList(), "Id", "Ip", model.SubIPId);
                ViewBag.serverPurposeIds = new SelectList(ServerPurposeService.GetList(), "serverPurposeId", "serverPurposeName");
                //List<SelectListItem> items = new List<SelectListItem> { new SelectListItem { Value = "0", Text = "无产权" }, new SelectListItem { Value = "1", Text = "送产权" } };
                //ViewBag.equitys = new SelectList(items, "Value", "Text", model.Equity);
                //if (model.UseNumber <= 0)
                //{
                //    List<SelectListItem> uNO = new List<SelectListItem>();
                //    ViewBag.uNOs = new SelectList(uNO);
                //}
                //else
                //{
                //    ViewBag.uNOs = new SelectList(CabinetManageService.useNO(model.CabinetId), model.UseNumber);
                //}
                //if (!model.ServerId.HasValue)
                //{
                //    List<SelectListItem> code = new List<SelectListItem>();
                //    ViewBag.Codes = new SelectList(code);
                //}
                //else
                //{
                //    ViewBag.Codes = new SelectList(AssetService.ServerList(model.CompanyId), model.ServerId);
                //}

                CabinetClass c = CabinetClassService.GetCabinetClass(1);
                List<SelectListItem> citems = new List<SelectListItem> { new SelectListItem { Value = c.IsSendProperty, Text = "送产权" }, new SelectListItem { Value = c.IsNoProperty, Text = "无产权" } };
                ViewBag.Propertys = new SelectList(citems, "Value", "Text", model.Property);
                CabinetClass Tc = CabinetClassService.GetCabinetClass(2);
                List<SelectListItem> Tcitems = new List<SelectListItem> { new SelectListItem { Value = Tc.IsSendProperty, Text = "送产权" }, new SelectListItem { Value = Tc.IsNoProperty, Text = "无产权" } };
                ViewBag.TPropertys = new SelectList(Tcitems, "Value", "Text");
                return View(model);
            }
            oldAssetModel assetModel = new oldAssetModel();
            BinaryFormatter binaryFormatter = new BinaryFormatter();
            try
            {
                using (Stream stream = System.IO.File.OpenRead(System.AppDomain.CurrentDomain.BaseDirectory.ToString()+"oldAsset.dat"))
                {
                    assetModel = (oldAssetModel)binaryFormatter.Deserialize(stream);
                }
            }
            catch (Exception ex)
            {
                TempData["alert"] = "error#请先添加已有资产";
                return RedirectToAction("index", "Assetes");
            }
            oldServerInfo oldInfo = new oldServerInfo();
            try
            {
                using (Stream stream = System.IO.File.OpenRead(System.AppDomain.CurrentDomain.BaseDirectory.ToString()+"oldServerInfo.dat"))
                {
                    oldInfo = (oldServerInfo)binaryFormatter.Deserialize(stream);
                }
            }
            catch (Exception ex)
            {
                TempData["alert"] = "error#请先完善已有资产信息";
                return RedirectToAction("index", "Assetes");
            }
            if (model.PurposeId != 1)
            {
                model.Property = null;
            }
            if (model.PurposeId != 2)
            {
                model.TProperty = null;
            }
            try {
                //添加资产，状态为出库
                var mapperconfig = new MapperConfiguration(cfg => { cfg.CreateMap<oldAssetModel, zichan.Entity.Asset>(); });
                IMapper mapper = mapperconfig.CreateMapper();
                zichan.Entity.Asset asset = mapper.Map<oldAssetModel, zichan.Entity.Asset>(assetModel);
                //asset.AssetClassId = 1;
                asset.BuyTime = DateTime.Parse("2016/1/1");
                asset.CompanyId = model.CompanyId;
                asset.Name = model.Name;
                asset.StatusTime = DateTime.Now;
                asset.UploadTime = DateTime.Now;
               
                asset.UserId = model.UserId;
                asset.Status = (int)AsseteStatu.InUse;
                asset.Number = 1;
                int id = AssetService.AddOldAsset(asset);
                asset = AssetService.Get(id);//id是否有值
                                             //asset.Code = asset.Code = assetModel.Identitys + DateTime.Now.Year + asset.Id.ToString().PadLeft(4, '0');
                                             //AssetService.Edit(asset);
                                             //完善服务器信息
                var mapperconfig2 = new MapperConfiguration(cfg => { cfg.CreateMap<oldServerInfo, zichan.Entity.ServerInfo>(); });
                IMapper mapper2 = mapperconfig2.CreateMapper();
                zichan.Entity.ServerInfo serverInfo = mapper2.Map<oldServerInfo, zichan.Entity.ServerInfo>(oldInfo);
                serverInfo.AssetId = asset.Id;
                serverInfo.SerialNumber = model.SerialNumber;
                serverInfo.U_Number = model.UseNumber;
                string err2 = ServerInfoService.Add(serverInfo);
                if (!string.IsNullOrEmpty(err2))
                {
                    AssetService.DeleteOldAssete(asset.Id);
                    TempData["alert"] = "error#完善信息错误";
                    return RedirectToAction("Index");
                }

                //异步委托


                model.Code = asset.Code;
                var mapperconfig3 = new MapperConfiguration(cfg => { cfg.CreateMap<OldPutawayModel, PutawayServer>(); });
                IMapper mapper3 = mapperconfig3.CreateMapper();
                PutawayServer server = mapper3.Map<OldPutawayModel, PutawayServer>(model);
                string err = CabinetManageService.AddPutAway(server);
                if (string.IsNullOrEmpty(err))
                {
                    TempData["alert"] = "success#上架成功";
                    CabinetUseLog log = new CabinetUseLog();
                    log.Time = DateTime.Now;
                    log.cabinetId = model.CabinetId;
                    log.Operation = "服务器上架：" + model.Code + "-" + model.UseNumber + "U-" + CompanyClassService.GetCompany(model.CompanyId).Name + "-" + CabinetClassService.GetCabinetClass(model.PurposeId).Purpose + "-" + IPService.Get(model.IPId).Ip + "-" + IPService.GetSubIp(model.SubIPIds);
                    CabinetUseLogService.Add(log);
                    MachineRoomLogService.Add(3, log.Operation);
                }
                else
                {
                    TempData["alert"] = "error#" + err;
                }
                //}
                return RedirectToAction("index");
            }
            catch(Exception ex)
            {
                TempData["alert"] = "error#无法上架";
                return RedirectToAction("index");
            }
        }
        public ActionResult TemporaryEdit(int id)
        {
            ViewBag.tId = id;
            ViewBag.serverPurposeIds = new SelectList(ServerPurposeService.GetList(), "serverPurposeId", "serverPurposeName");
            return View();
        }
        [HttpPost]
        public ActionResult TemporaryEdit(TemporaryEditModel model)
        {
            if(!ModelState.IsValid)
            {
                ViewBag.tId = model.tId;
                ViewBag.serverPurposeIds = new SelectList(ServerPurposeService.GetList(), "serverPurposeId", "serverPurposeName");
                return View(model);
            }
            string code = CabinetManageService.Get(model.tId).Code;
            int id =Convert.ToInt32(code.Substring(code.Length - 4, 4));
            string err = AssetService.EditPurpose(id, model.purposeId, model.serverPurposeSubId);
            if(string.IsNullOrEmpty(err))
            {
                TempData["alert"] = "success#修改用途成功";
            }
            else
            {
                TempData["alert"] = "error#修改失败";
            }
           
            return RedirectToAction("Index");
        }
        #endregion
        // static void ThreaToInfo()
        //{
        //    var mapperconfig2 = new MapperConfiguration(cfg => { cfg.CreateMap<oldServerInfo, zichan.Entity.ServerInfo>(); });
        //    IMapper mapper2 = mapperconfig2.CreateMapper();
        //    zichan.Entity.ServerInfo serverInfo = mapper2.Map<oldServerInfo, zichan.Entity.ServerInfo>(oldInfo);
        //    serverInfo.AssetId = asset.Id;
        //    serverInfo.SerialNumber = model.SerialNumber;
        //    serverInfo.U_Number = model.UseNumber;
        //    string err2 = ServerInfoService.Add(serverInfo);
        //}
    }
}